package com.barcamp.bean;

import java.util.ArrayList;
import java.util.List;
import javax.faces.component.UIData;
import javax.faces.model.SelectItem;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.barcamp.entity.EventType;
import com.barcamp.manager.EventTypeManager;
import com.exception.PersistenceException;

public class EventTypeBean{
	private EventType type = new EventType();
	private List<EventType> listevent = new ArrayList<EventType>();
	private UIData uiDataTable;
	private List<SelectItem> selectEvent;
	
	public List<SelectItem> getSelectEvent() {
		try {
			getListevent();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		selectEvent = new ArrayList<SelectItem>();
	    for(int i=0; i < listevent.size(); i++){          
	        selectEvent.add(new SelectItem(listevent.get(i).getId(), listevent.get(i).getName(), listevent.get(i).getDescription()));
	    }
	        return selectEvent;
	}
	public void setSelectEvent(List<SelectItem> selectEvent) {
		this.selectEvent = selectEvent;
	}
	public EventType getType() {
		return type;
	}
	public void setType(EventType type) {
		this.type = type;
	}
	public List<EventType> getListevent() throws PersistenceException {
		try {
	        listevent = getManager().findAllEvent();
	    } catch (PersistenceException e) {
	        e.printStackTrace();
	    } 
	        return listevent;
	}
	public void setListevent(List<EventType> listevent) {
		this.listevent = listevent;
	}
	public UIData getUiDataTable() {
		return uiDataTable;
	}
	public void setUiDataTable(UIData uiDataTable) {
		this.uiDataTable = uiDataTable;
	}

	public String insertNew()throws PersistenceException{
		try{getManager().insertNew(type);}
		catch(PersistenceException e){
			e.printStackTrace();
		}
		return "insert";
	}
	public String attend(){
		//type=(EventType)uiDataTable.getRowData();
		return "goAttend";
	}
	public String absent(){
		type=(EventType)uiDataTable.getRowData();
		return "goAbsent";
	}
	public String edit() {
		type=(EventType)uiDataTable.getRowData();
		return "goUpdate";
	}
	public String update()throws PersistenceException{
		try{
			getManager().updateType(type);}
		catch(PersistenceException e){
			e.printStackTrace();
		}
		return "update";
	}
	public String preDelete() throws PersistenceException{
		type=(EventType)uiDataTable.getRowData();
		return "goDelete";
	}
	public String delete()throws PersistenceException
	{
		try{
			getManager().delete(type.getId());}
		catch(PersistenceException e){e.printStackTrace();}
		return "delete";
	}
	public String cancel(){
		return "cancel";
	}
	public EventTypeManager getManager()
	{
		EventTypeManager mgr = null;
		XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));
		mgr =(EventTypeManager)factory.getBean("EventTypeManager");
		return mgr;
	}
}
